package com.shenmazong.servereduschool.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shenmazong.servereduschool.pojo.TbCollage;
import com.shenmazong.servereduschool.mapper.TbCollageMapper;
import com.shenmazong.servereduschool.service.ITbCollageService;
import com.shenmazong.utils.ResultResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


@Service
@Primary
public class TbCollageServiceImpl extends ServiceImpl<TbCollageMapper, TbCollage> implements ITbCollageService {

    @Autowired
    private TbCollageMapper collageMapper;


    @Override
    public ResultResponse registerCollage(TbCollage collage) {
        try {
            collage.setCreateTime(new Date());
            collage.setUpdateTime(new Date());
            int i = collageMapper.insert(collage);
            if(i<1){
                return ResultResponse.FAILED(-1,"添加失败");
            }
        } catch (Exception e) {
        }
        return ResultResponse.SUCCESS("添加成功");
    }


    @Override
    public ResultResponse updateCollage(TbCollage collage) {
        try {
            collage.setUpdateTime(new Date());
            int i = collageMapper.updateById(collage);
            if(i<1){
                return ResultResponse.FAILED(-1,"修改失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultResponse.SUCCESS();
    }

    @Override
    public ResultResponse deleteCollage(TbCollage tbCollage) {
        try {
            collageMapper.deleteById(tbCollage.getCollageId());
        } catch (Exception e) {
            e.printStackTrace();
            return ResultResponse.FAILED();
        }
        return ResultResponse.SUCCESS();
    }

    @Override
    public PageInfo findCollageList(Integer pageNum, Integer pageSize, Object tbCollage) {
        PageHelper.startPage(pageNum,pageSize);
        List<TbCollage> tbCollages=collageMapper.findCollageList(tbCollage);
        PageInfo<TbCollage> pageInfo = new PageInfo<>(tbCollages);
        return pageInfo;
    }

    @Override
    public ResultResponse queryByCollageId(TbCollage tbCollage) {
        TbCollage collage = collageMapper.selectById(tbCollage.getCollageId());
        return ResultResponse.SUCCESS(collage);

    }

    @Override
    public ResultResponse deleteAll(List<TbCollage> ids) {
        if(ids.size()<=0){
            return ResultResponse.FAILED(-1,"至少选在一个");
        }
        for (TbCollage tbCollage : ids) {
            collageMapper.deleteById(tbCollage.getCollageId());
        }
        return ResultResponse.SUCCESS("删除成功");
    }

    @Override
    public ResultResponse findCollage() {
        List<TbCollage> collages = collageMapper.selectList(null);
        return ResultResponse.SUCCESS(collages);
    }
}
